package com.company.project.robot.service.impl;

import com.alibaba.fastjson.JSON;
import com.company.project.robot.entity.SendRecordEntity;
import com.company.project.robot.mapper.SendRecordMapper;
import com.company.project.robot.sdk.Api;
import com.company.project.robot.service.SendRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;


@Service("sendRecordService")
@Slf4j
public class SendRecordServiceImpl extends ServiceImpl<SendRecordMapper, SendRecordEntity> implements SendRecordService {


    @Value("${cat.http.api}")
    private String apiUrl;



    /**
     * 保存并发送文件至微信群
     * @param sendRecord
     */
    @Override
    public void saveSendFile(SendRecordEntity sendRecord) {
        log.info(JSON.toJSONString(sendRecord));
        if(sendRecord.getSendType() == 1){
            send(sendRecord);
            Date d= new Date();
            sendRecord.setSendTime(d);
            this.save(sendRecord);
        }
        if (sendRecord.getSendType() == 0){
            sendRecord.setStatus(0);
            this.save(sendRecord);
        }
    }

    /**
     * 定时发送文件并修改状态
     * @param sendRecord
     */
    @Override
    public void updateSendFile(SendRecordEntity sendRecord){
        send(sendRecord);
        this.updateById(sendRecord);
    }

    public void send(SendRecordEntity sendRecord){
        String[] wechatGroups = sendRecord.getWechatGroupId().split(",");
        switch (sendRecord.getFileType()) {
            case 1:
                log.info("===========发送文本============");
                for (String wechatGroup : wechatGroups) {
                    try {
//                        Api.send_text_msg(apiUrl, sendRecord.getRobotId(), wechatGroup, sendRecord.getFileUrl());
                        sendRecord.setStatus(1);
                    } catch (Exception ex) {
                        sendRecord.setStatus(0);
                        log.error(ex.toString());
                    }
                }
                break;
            case 2:
                log.info("===========发送图片============");
                for (String wechatGroup : wechatGroups) {
                    String[] imgs = sendRecord.getFileUrl().split(",");
                    for (String img : imgs) {
                        try {
                            Api.send_image_msg(apiUrl, sendRecord.getRobotId(), wechatGroup, img);
                            sendRecord.setStatus(1);
                        } catch (Exception ex) {
                            sendRecord.setStatus(0);
                            log.error(ex.toString());
                        }
                    }
                }
                break;
            case 3:
                log.info("===========发送视频============");
                for (String wechatGroup : wechatGroups) {
                    String[] videos = sendRecord.getFileUrl().split(",");
                    for (String video : videos) {
                        try {
                            Api.send_video_msg(apiUrl, sendRecord.getRobotId(), wechatGroup, URLEncoder.encode(video,"UTF-8"));
                            sendRecord.setStatus(1);
                        } catch (Exception ex) {
                            sendRecord.setStatus(0);
                            log.error(ex.toString());
                        }
                    }
                }
                break;
            case 0:
                log.info("===========发送文件============");
                for (String wechatGroup : wechatGroups) {
                    String[] files = sendRecord.getFileUrl().split(",");
                    for (String file : files) {
                        try {
                            Api.send_file_msg(apiUrl, sendRecord.getRobotId(), wechatGroup, URLEncoder.encode(file,"UTF-8"));
                            sendRecord.setStatus(1);
                        } catch (Exception ex) {
                            sendRecord.setStatus(0);
                            log.error(ex.toString());
                        }
                    }
                }
                break;
        }
    }

}
